package com.star.controller.business;

import com.star.dto.OrdersCancelDTO;
import com.star.dto.OrdersPageQueryDTO;
import com.star.dto.OrdersRejectionDTO;
import com.star.result.PageResult;
import com.star.result.Result;
import com.star.service.OrderService;
import com.star.vo.OrderStatisticsVO;
import com.star.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("businessOrderController")
@RequestMapping("/business/order")
@Slf4j
@Api(tags ="商家端订单管理相关接口")
public class OrdersController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单搜索:{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> getOrderStatistics(Long businessId) {
        log.info("各个状态的订单数量统计");
        return Result.success(orderService.getOrderStatistics(businessId));
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("取消订单{}", ordersCancelDTO);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 接收订单
     * @param id
     * @return
     */
    @PutMapping("/confirm/{id}")
    @ApiOperation("接收订单")
    public Result<String> confirm(@PathVariable Long id){
        log.info("接收订单{}", id);
        orderService.confirm(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> complete(@PathVariable Long id){
        log.info("完成订单{}", id);
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 拒绝订单
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("拒绝订单")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("拒绝订单{}", ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> delivery(@PathVariable Long id){
        log.info("派送订单{}", id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("订单详情")
    public Result<OrderVO> detail(@PathVariable Long id){
        log.info("订单详情{}", id);
        OrderVO orderVO = orderService.businessOrderDetail(id);
        return Result.success(orderVO);
    }

}
